﻿Imports System.Data.SqlClient

Public Class frmCSF
    Dim _filter As Boolean = True
    Private Sub frmCSF_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ECollegeDataSet.CSF_View' table. You can move, or remove it, as needed.
        Me.CSF_ViewTableAdapter.Fill(Me.ECollegeDataSet.CSF_View)
        'TODO: This line of code loads data into the 'ECollegeDataSet.Teacher' table. You can move, or remove it, as needed.
        Me.TeacherTableAdapter.Fill(Me.ECollegeDataSet.Teacher)
        'TODO: This line of code loads data into the 'ECollegeDataSet.Subject' table. You can move, or remove it, as needed.
        Me.SubjectTableAdapter.Fill(Me.ECollegeDataSet.Subject)
        'TODO: This line of code loads data into the 'ECollegeDataSet.Section' table. You can move, or remove it, as needed.
        Me.SectionTableAdapter.Fill(Me.ECollegeDataSet.Section)
        'TODO: This line of code loads data into the 'ECollegeDataSet.CSF_View' table. You can move, or remove it, as needed.
        'Me.CSF_ViewTableAdapter.Fill(Me.ECollegeDataSet.CSF_View)
        'Param1ToolStripTextBox.Text = ComboBox1.Text.Trim
        Me.CSF_ViewTableAdapter.FillBy(Me.ECollegeDataSet.CSF_View, ComboBox1.Text.Trim)
        Me.DataGridView1.Invalidate()
        V_CourseStructureTableAdapter.Fill(ECollegeDataSet.V_CourseStructure, Me.ComboBox1.SelectedValue)
        cbCSSubject.Invalidate()


        Me.Refresh()

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sQry As String = ""
        If TextBox1.Text.Trim > 0 Then
            sQry = "Exec UpdateCSF @csfid=" & TextBox1.Text.Trim & ",@facid=" & Me.ComboBox3.SelectedValue
            Dim cn As New SqlConnection
            cn.ConnectionString = My.Settings.eCollegeConnectionString
            cn.Open()
            ' MsgBox(sQry)
            Dim cmd As New SqlCommand(sQry, cn)
            cmd.ExecuteNonQuery()
            If cn.State = ConnectionState.Open Then cn.Close()
        Else
            'sQry = "INSERT INTO [L_Section_Subject_Faculty] ([Section_Id] ,[Subject_Code],[Faculty_Id],[IsRemoved],[Assign_Date] ,[SessionID])         VALUES (" & Me.ComboBox1.SelectedValue & " ,'" & Me.ComboBox2.SelectedValue & "' ," & Me.ComboBox3.SelectedValue & ",0 ,Current_timestamp  ,11 )"
            sQry = "INSERT INTO [CSF] ([Section_Id],[Subject_Id] ,[Subject_Code],[Faculty_Id],[IsRemoved],[LA],[TA],[PA]) VALUES (" & Me.ComboBox1.SelectedValue & " ,'" & Me.cbCSSubject.SelectedValue & "' ,'" & Me.cbCSSubject.Text.Trim & "' ," & Me.ComboBox3.SelectedValue & ",0," & Me.nLA.Value & " ," & Me.nTA.Value & " ," & Me.nPA.Value & ")"
            Dim cn As New SqlConnection
            cn.ConnectionString = My.Settings.eCollegeConnectionString
            cn.Open()

            Dim cmd As New SqlCommand(sQry, cn)
            ' MsgBox(sQry)
            cmd.ExecuteNonQuery()
            If cn.State = ConnectionState.Open Then cn.Close()
        End If
        ' Me.DialogResult = System.Windows.Forms.DialogResult.OK
        'Me.CSF_ViewTableAdapter.Fill(Me.ECollegeDataSet.CSF_View)
        ComboBox1.Focus()
        Me.CSF_ViewTableAdapter.FillBy(Me.ECollegeDataSet.CSF_View, Param1ToolStripTextBox.Text)
        Me.DataGridView1.Invalidate()
        Me.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = 0
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value

    End Sub

    Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillByToolStripButton.Click
        Try
            Me.CSF_ViewTableAdapter.FillBy(Me.ECollegeDataSet.CSF_View, Param1ToolStripTextBox.Text)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Param1ToolStripTextBox.Text = ComboBox1.Text.Trim
        Me.CSF_ViewTableAdapter.FillBy(Me.ECollegeDataSet.CSF_View, Param1ToolStripTextBox.Text)
        Me.DataGridView1.Invalidate()
        V_CourseStructureTableAdapter.Fill(ECollegeDataSet.V_CourseStructure, Me.ComboBox1.SelectedValue)
        cbCSSubject.Invalidate()


        Me.Refresh()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim fSub As New frmSubjectQuick
        fSub.ShowDialog()
        Me.SubjectTableAdapter.Fill(Me.ECollegeDataSet.Subject)
        Me.cbCSSubject.Invalidate()
        Me.Refresh()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim fSub As New frmEmployee
        fSub.ShowDialog()
        Me.TeacherTableAdapter.Fill(Me.ECollegeDataSet.Teacher)
        Me.ComboBox3.Invalidate()
        Me.Refresh()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        _filter = Not _filter
        If _filter Then
            Me.cbCSSubject.DataSource = BindingSource1
            Me.cbCSSubject.DisplayMember = "subject_code"
            Me.cbCSSubject.ValueMember = "id"
            Me.cbCSSubject1.DataSource = BindingSource1
            Me.cbCSSubject1.DisplayMember = "subject_name"
            Me.nLA.DataBindings.Clear()
            Me.nLA.DataBindings.Add("Value", BindingSource1, "L")
            Me.nTA.DataBindings.Clear()
            Me.nTA.DataBindings.Add("Value", BindingSource1, "T")
            Me.nPA.DataBindings.Clear()
            Me.nPA.DataBindings.Add("Value", BindingSource1, "P")
            Button5.Text = "Remove Filter"
        Else
            Me.cbCSSubject.DataSource = SubjectBindingSource
            Me.cbCSSubject.DisplayMember = "code"
            Me.cbCSSubject.ValueMember = "id"
            Me.cbCSSubject1.DataSource = SubjectBindingSource
            Me.cbCSSubject1.DisplayMember = "name"
            Me.nLA.DataBindings.Clear()
            Me.nLA.DataBindings.Add("Value", SubjectBindingSource, "L")
            Me.nTA.DataBindings.Clear()
            Me.nTA.DataBindings.Add("Value", SubjectBindingSource, "T")
            Me.nPA.DataBindings.Clear()
            Me.nPA.DataBindings.Add("Value", SubjectBindingSource, "P")

            'Me.nLA.DataBindings.Clear()
            'Me.nLA.DataBindings.Add(New Binding("Value", SubjectBindingSource, "L", True, DataSourceUpdateMode.OnPropertyChanged))

            Button5.Text = "Filter Subjecs"
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text = 0 Then
            Button6.Visible = False
        Else
            Button6.Visible = True
        End If
    End Sub

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
        Dim sQry As String = ""
    

        sQry = "Delete From [CSF] Where CSF_ID=" & Val(TextBox1.Text)
            Dim cn As New SqlConnection
            cn.ConnectionString = My.Settings.eCollegeConnectionString
            cn.Open()

            Dim cmd As New SqlCommand(sQry, cn)
            ' MsgBox(sQry)
            cmd.ExecuteNonQuery()
            If cn.State = ConnectionState.Open Then cn.Close()

        ComboBox1.Focus()
        TextBox1.Text = 0
        Me.CSF_ViewTableAdapter.FillBy(Me.ECollegeDataSet.CSF_View, Param1ToolStripTextBox.Text)
        Me.DataGridView1.Invalidate()
        Me.Refresh()
    End Sub
End Class